﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Platform;
using MiniFox.Utility;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;
using Sys.Core;
using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;

namespace Sys.Controllers
{
    /// <summary>
    /// 字典
    /// </summary>
    [NoCache, ValidationFilter, Area(GlobalConstants.AREA_NAME)]
    public class DictionaryController : MvcControllerBase
    {
        [AutoWired]
        protected IDictionaryService Component { get; set; }

        /// <summary>
        /// 字典集
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet, AllowAnonymous]
        public Response<DictionaryGroupVo> Group([FromRoute(Name = GlobalKey.NAME)] string appId, [FromQuery] string group)
        {
            DictionaryGroupVo result = this.Component.Item(appId, group);
            return result.CreateResponse();
        }
        /// <summary>
        /// 字典组
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<ResultObject<IEnumerable<DictionaryGroupVo>>> Query([FromRoute(Name = GlobalKey.NAME)] string appId, [FromBody] PagedQueryObject<DictionaryQuery> request)
        {
            var result = this.Component.Query(appId, request);
            return result.CreateResponse();
        }
        /// <summary>
        /// 新建字典集
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<DictionaryGroupVo> Create([FromRoute(Name = GlobalKey.NAME)] string appId, [FromBody] DictionaryGroupDto dto)
        {
            DictionaryGroupVo result = this.Component.Create(appId, dto);
            return result.CreateResponse();
        }
        /// <summary>
        /// 更新字典集
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<DictionaryGroupVo> Update([FromRoute(Name = GlobalKey.NAME)] string appId, [FromBody] DictionaryGroupDto dto)
        {
            DictionaryGroupVo result = this.Component.Update(appId, dto);
            return result.CreateResponse();
        }
        /// <summary>
        /// 删除字典集
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Response Remove([FromRoute(Name = GlobalKey.NAME)] string appId, [FromBody] string[] groups)
        {
            this.Component.Remove(appId, groups);
            return new();
        }
        /// <summary>
        /// 切换字典项
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Response Toggle([FromRoute(Name = GlobalKey.NAME)] string appId, [FromQuery] string group, [FromQuery] string val)
        {
            this.Component.ToggleItem(appId, group, val);
            return new();
        }
    }
}
